package com.yunchang.springboot.quatrz.job.history;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunchang.springboot.mapper.base.IBaseMwsReportLogMapper;
import com.yunchang.springboot.models.Area;
import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.models.log.BaseMwsReportLogDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.detail.GetReportProcessDetail;
import com.yunchang.springboot.utils.CryptoUtil;
import com.yunchang.springboot.utils.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.datatype.XMLGregorianCalendar;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author sjd
 * @date 2020/8/19 0030  15:47
 * @description: mws报告的历史数据拉取功能
 */
@DisallowConcurrentExecution
public class ReportReimbursementsHistoryJob implements Job {
    @Resource
    GetReportProcessDetail getReportProcessDetail;

    @Resource
    IBaseMwsReportLogMapper baseMwsReportLogMapper;

    @Getter
    @Setter
    private String reportName;

    @Getter
    @Setter
    private String reportType;

    /**
     * 报告获取时间范围
     */
    @Getter
    @Setter
    private String timeScope;

    @Getter
    @Setter
    private Integer getReportArea;

    @Getter
    @Setter
    private Integer getHistoryTime;

    @Getter
    @Setter
    private Integer reportInfoId;

    @Override
    public void execute(JobExecutionContext context) {
        try {
            // 调用获取报告的方法，指定报告名称及报告类型和时间范围
            List<Map<String, XMLGregorianCalendar>> reportTimeScopeList = getReportProcessDetail.getReportHistoryTimeScope(getHistoryTime);
            // 根据任务名获取店铺及区域
            List<Map<Shop, Area>> shopAreaMapList = getReportProcessDetail.getShopAndAreaInfoByGetReportArea(getReportArea);
            for (Map<Shop, Area> shopAreaMap : shopAreaMapList) {
                for (Shop shopKey : shopAreaMap.keySet()) {
                    //按每两个月的时间间隔去获取历史数据
                    for (Map<String, XMLGregorianCalendar> reportTimeScopeMap : reportTimeScopeList) {
                        String reportBeginTimeString = DateUtil.transLocalDateTimeToDateString(DateUtil.transXMLGregorianCalendarToLocalDateTime(reportTimeScopeMap.get("reportBeginTime")));
                        String reportEndTimeString = DateUtil.transLocalDateTimeToDateString(DateUtil.transXMLGregorianCalendarToLocalDateTime(reportTimeScopeMap.get("reportEndTime")));
                        BaseMwsReportLogDO childMwsReportLog = new BaseMwsReportLogDO()
                                .setParentId(0L)
                                .setSellerId(CryptoUtil.encode(shopKey.getSellerId()))
                                .setCountryCode(shopAreaMap.get(shopKey).getMwsCountryCode())
                                .setReportInfoId(reportInfoId)
                                .setReportType(reportType)
                                .setAccessMethod(2)
                                .setStartDate(reportBeginTimeString)
                                .setEndDate(reportEndTimeString)
                                .setCreateTime(DateUtil.getNowDateTime())
                                .setModifyTime(DateUtil.getNowDateTime())
                                .setIsDelete(0);
                        saveRequiredNewLog(childMwsReportLog);
                        //新增之后再从数据库中查找出这条数据，主要是为了获得自增id，确定唯一性
                        BaseMwsReportLogDO mwsReportLogDO = baseMwsReportLogMapper.selectOne(
                                new LambdaQueryWrapper<BaseMwsReportLogDO>()
                                        .eq(BaseMwsReportLogDO::getParentId, childMwsReportLog.getParentId())
                                        .eq(BaseMwsReportLogDO::getSellerId, childMwsReportLog.getSellerId())
                                        .eq(BaseMwsReportLogDO::getCountryCode, childMwsReportLog.getCountryCode())
                                        .eq(BaseMwsReportLogDO::getReportInfoId, childMwsReportLog.getReportInfoId())
                                        .eq(BaseMwsReportLogDO::getReportType, childMwsReportLog.getReportType())
                                        .eq(BaseMwsReportLogDO::getAccessMethod, childMwsReportLog.getAccessMethod())
                                        .eq(BaseMwsReportLogDO::getStartDate, childMwsReportLog.getStartDate())
                                        .eq(BaseMwsReportLogDO::getEndDate, childMwsReportLog.getEndDate())
                                        .eq(BaseMwsReportLogDO::getCreateTime, childMwsReportLog.getCreateTime())
                                        .eq(BaseMwsReportLogDO::getModifyTime, childMwsReportLog.getModifyTime())
                                        .eq(BaseMwsReportLogDO::getIsDelete, childMwsReportLog.getIsDelete()));
                        // 异步调用获取报告方法
                        getReportProcessDetail.reportDetail(reportName, reportInfoId, reportType, timeScope, reportTimeScopeMap, shopKey, shopAreaMap.get(shopKey), mwsReportLogDO);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    void saveRequiredNewLog(BaseMwsReportLogDO childMwsReportLog) {
        baseMwsReportLogMapper.insert(childMwsReportLog);
    }
}
